<?php

    function classLoader($class)
    {
    	if(substr($class, 0, 6) !== "Devmod")
    		return;
    		
    	if(strpos($class, 'method_') === false)
		{
			require_once("src/{$class}.class.php");
		} else {
			require_once("methods/{$class}.class.php");
		}
    }

    spl_autoload_register('classLoader');

	/**
	 * DevMod (developement modules) class, create modules on the fly!
	 * @package devmod
	 * @author bsuter
	 * @version 2
	 */
	Class Yomm_Devmod_Devmod Extends Devmod_base { 

		public function __construct($sql_table = null, $theme = null)
		{
			parent::__construct();
			
			if(!is_null($sql_table) && !is_null($theme))
			{
				$this->import_config(array(
					'smarty_vars' => array(), 
					'edit' => array(), 
					'del' => array(), 
					'list' => array(), 
					'frontend_list' => array(), 
					'add' => array(), 
					'frontend_add' => array(), 
					'theme' => $theme, 
					'sql_table' => $sql_table, 
					'callback_url' => Yomm_Config::URL_GRID_CALLBACK,
					'structure' => array(),
					'env' => array()
				));
			}
		}

		public function get_env_vars()
		{
			return $this->env_vars;
		}
		
		public function import_config($array, $methods_objs = false)
		{
			$this->config = $array;	
			
			if($methods_objs !== false)
			{
				$this->method_objects = unserialize($methods_objs);
			}
			
			if(isset($this->config['view_obj']))
			{
				$this->view = unserialize($this->config['view_obj']);
				$this->view->template_dir = 'theme/'. $this->config['theme'] .'/tpl/';
				
			} else {
				$this->view = new Yomm_View($this->config['theme']);
			}
			
			$this->view->clearAssign('tpl_addons');
			
			$this->view->assign('_languages', $this->db->select("cms_language", "*", false, true));
			
			$this->view->assign('grid_check_sum', $this->generate_checksum());
			
			if(isset($this->config['sql_table']))
			{
				$this->table_struct = $this->analyze_table($this->config['sql_table']);
				$this->config['structure'] = $this->table_struct; // new push this into table config
				$this->view->assign('table_struct', $this->table_struct);
				
				/* list if its an devmod build not internally */
				
				$db = $this->db->select("dev_sql", "id", "WHERE name='{$this->config['sql_table']}'", false);
				
				if(count($db) == 1)
				{
					$dev_sql_id = $db->id;
				} else {
					$dev_sql_id = strlen($this->config['sql_table']);
				}
				
				$this->config['env'] = array(
					'dev_sql_id' => $dev_sql_id,
					'dev_mods' => array()
				);
			}
		}
		
		public function get_config()
		{
			return $this->config;
		}
		
		public function analyze_table($table)
		{
			$struct = array();
			$result = $this->db->query("SHOW COLUMNS FROM $table");
			while($data = mysqli_fetch_assoc($result))
			{
				if($data['Type'] == "text" || $data['Type'] == "longtext")
				{
					$value = "text";
				} else {
					$value = "varchar";
				}
				
				$struct[$data['Field']] = $value;
			}
			
			return $struct;
		}
		
		public function get_config_by_type($type)
		{
			return $this->config[$type];
		}
		
		public function getAllViewVars()
		{
			return $this->view->getTemplateVars();
		}
		
		/**
		 * set the config values in the session cmobined with a checksume for the sql table
		 * only, for the valid confiuration file.
		 */
		public function __destruct()
		{
			if(count($this->config) > 0)
			{
				//$this->config['view_obj'] = serialize($this->view);
				$_SESSION[$this->generate_checksum()]['grid_config'] = $this->config;
				$_SESSION[$this->generate_checksum()]['grid_methods'] = serialize($this->method_objects);
			}
		}
	}

	
	/* devmod functions */
	
	/*
	 * experimental function for devmod with castField 'date'
	 */
	function cast_date_convert($form)
	{
		if($form == "%%value%%") return null; // TODO: find the source of the loop through
		
		$ds = explode("-", $form);
		
		return mktime(0,0,0,$ds[1],$ds[0],$ds[2]);
	}
	
	function sendmail($to_mail, $to_name, $subject, $text, $sender_name = null, $sender_mail = null)
	{
		$mail = new Yomm_Mail_Send($to_mail, $to_name, $subject, $text);
		
		if(!is_null($sender_name) && !is_null($sender_mail))
		{
			$mail->SetFrom($sender_mail, $sender_name);
		}
		
		$mail->send();
	}